﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using EFFC.Net.Common;
using EFFC.Net.Exceptions;

namespace EFFC.Net.Builder.WebPage
{
    public abstract class HttpBaseHandler:IHttpHandler
    {
        private HttpResponse _response = null;
        /// <summary>
        /// 连接跳转
        /// </summary>
        /// <param name="url"></param>
        public void Redirect(string url)
        {
            if (GlobalCommon.SecurityStrategy.IsValidUrl(url))
            {
                _response.Redirect(HttpUtility.HtmlEncode(url), false);
            }
            else
            {
                throw new BizException(url + ":Is not valid URL");
            }
        }
        /// <summary>
        /// 连接跳转
        /// </summary>
        /// <param name="url"></param>
        /// <param name="endResponse">是否结束Response</param>
        public void Redirect(string url, bool endResponse)
        {
            if (GlobalCommon.SecurityStrategy.IsValidUrl(url))
            {
                _response.Redirect(HttpUtility.HtmlEncode(url), endResponse);
            }
            else
            {
                throw new BizException(url + ":Is not valid URL");
            }
        }

        private Dictionary<string, string> _requeststrings = null;
        public Dictionary<string, string> RequestParameters
        {
            get { return _requeststrings; }
        }

        #region IHttpHandler Members

        public abstract bool IsReusable
        {
            get;
        }

        public abstract void DoProcessRequest(HttpContext context);

        #endregion

        #region IHttpHandler Members


        public void ProcessRequest(HttpContext context)
        {
            try
            {
                _response = context.Response;
                _requeststrings = new Dictionary<string, string>();
                foreach (string s in context.Request.QueryString.Keys)
                {
                    _requeststrings.Add(s, HttpUtility.UrlDecode(context.Request[s]));
                }

                DoProcessRequest(context);
            }
            catch (Exception ex)
            {
                GlobalCommon.ExceptionProcessor.DoProcess(ex);
            }
        }

        #endregion
    }
}
